package com.haoma.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.haoma.entity.Website;
import com.haoma.mapper.WebsiteMapper;
import com.haoma.minio.MinIOConfig;
import com.haoma.minio.MinIOConfigProperties;
import com.haoma.service.WebsiteService;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

/**
 * Description:
 */
@Service
public class WebsiteServiceImpl implements WebsiteService {

    @Autowired
    private MinIOConfigProperties minIOConfigProperties;

    @Autowired
    private MinIOConfig minIOConfig;

    @Autowired
    private WebsiteMapper websiteMapper;

    /**
     * 获取图片路径
     *
     * @param multipartFile
     * @param type
     * @return
     */
    public String getImageUrl(MultipartFile multipartFile, Integer type) {
        try {
            // 连接客户端
            MinioClient minioClient = minIOConfig.buildMinioClient();
            // 判断桶是否存在
            boolean isBucket = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minIOConfigProperties.getBucket()).build());
            if (!isBucket) {
                // 不存在创建一个
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(minIOConfigProperties.getBucket()).build());
            } else {
                // 提示程序员存在
                System.out.println("Bucket 'test' already exists.");
            }

            // 设置存储路径
            // 当天作为目录
            String date = DateUtil.format(new Date(), "yyyyMMdd");
            // 唯一id作为图片路径
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String fileName;
            if (type == 1) {
                // 网站头像
                // 组合＋业务
                fileName = "admin-website-avatar" + "/" + date + "/" + uuid + multipartFile.getOriginalFilename();
            } else {
                // 网站背景
                fileName = "admin-website-bg" + "/" + date + "/" + uuid + multipartFile.getOriginalFilename();
            }

            System.out.println(fileName);

            // 构建上传参数
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(minIOConfigProperties.getBucket())
                    .stream(multipartFile.getInputStream(), multipartFile.getSize(), -1)
                    .object(fileName)
                    .build();
            // 上传
            minioClient.putObject(putObjectArgs);
            // 将路径返回
            return minIOConfigProperties.getReadPath() + "/" + minIOConfigProperties.getBucket() + "/" + fileName;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 图片上传
     *
     * @param type
     * @param multipartFile
     * @return
     */
    public String fileUpload(Integer type, MultipartFile multipartFile) {
        String imageUrl = getImageUrl(multipartFile, type);
        return imageUrl;
    }

    @Override
    public void updateWebsite(Website website) {
        websiteMapper.updateById(website);
    }

    @Override
    public Website getWebsite() {
        Website website = websiteMapper.selectOne(new LambdaQueryWrapper<Website>().eq(Website::getId,1));
        return website;
    }
}
